﻿
function StoreEditController($scope, service, $routeParams, $location, $http, $filter, $validator) {
    $scope.storeExist = true;
    $scope.showTest = true;
    if ($routeParams.totalStaff < 1)
    {
        $scope.showTest = false;
    }

    // Aray Day
    $scope.Days = function () {
        var result = [];
        for (var i = 0; i < 30; i++) {
            result.push(i + 1);
        }
        return result;
    }
    // Array hours
    $scope.Hours = function () {
        var result = [];
        for (var i = 0; i < 24; i++) {
            result.push(i);
        }
        return result;
    }
    //Array Mins
    $scope.Mins = function () {
        var result = [];
        for (var i = 0; i < 60; i++) {
            result.push(i);
        }
        return result;
    }
    ///End
    //popup option
    var popupOptions = {
        appendTo: $("#popup-wrapper"),
        closeClass: 'b-close',
        speed: 300
    };
    //End

    $scope.path = "/StoreList";

    if ($routeParams.type == "Edit")
    {
       
        $scope.storeOder = $routeParams.order;
        $scope.totalStaff = $routeParams.totalStaff;

        $http.get(service.locationUrl + "/companystore/" + $routeParams.Id, {})
       .success(function (data) {
           $scope.rowsStore = data;
           $scope.yearMonth = $filter('date')(data.ClosingTime, 'yyyy-MM');
           $scope.day   = $filter('date')(data.ClosingTime, 'dd');
           $scope.hour = $filter('date')(data.ClosingTime, 'H');
           $scope.min = $filter('date')(data.ClosingTime, 'mm');

       })
       .error(function (err) {

           console.log("error", err);
       });
      
    }
    if ($routeParams.type == "New")
    {
        $scope.showTest = false;
        $scope.yearMonth = "2014-10"
        $scope.day = 25;
        $scope.hour = 2;
        $scope.min = 0;
        var closeDay = String($scope.yearMonth) + "-" + String($scope.day) + " " + String($scope.hour) + ":" + String($scope.min);
        $scope.rowsStore = 
            {
                Name: "",
                CompanyId: $routeParams.companyId,
                ClosingTime: closeDay,
                WelcomeMessage: "this is new store don't had the message yet. pls set the mesage for this store",
                RowStatus : 1
            }
    }
    // Edit or create new staff
    $scope.save = function () {

        $validator.validate($scope, "rowsStore")
           .success(function () {
               // popup detele
               $scope.title = "Do you want save that information?";
               $('.popup-delete-save').bPopup(popupOptions);
               // End

               //Action when you click btn save
               $scope.action = function () {
                   var closeDay = String($scope.yearMonth) + "-" + String($scope.day) + " " + String($scope.hour) + ":" + String($scope.min);
                   if ($routeParams.type == "Edit") {
                       $scope.title = "Do you want save that information?";
                       var storeObj =
                           {
                               Name: $scope.rowsStore.Name,
                               CompanyId: $scope.rowsStore.CompanyId,
                               ClosingTime: closeDay,
                               WelcomeMessage: $scope.rowsStore.WelcomeMessage
                           }
                       //Put method update obj store
                       $http.put(
                           service.locationUrl + "/companystore/" + $scope.rowsStore.Id,
                           storeObj)
                       .success(function () {
                           $location.path($scope.path);
                       })
                       .error(function () { });
                       // end put
                   }
                   if ($routeParams.type == "New") {
                       $scope.title = "Do you want create new store?";
                       var objStore = {
                           CompanyId: service.infoUser.Id,
                           Name: $scope.rowsStore.Name,
                       }
                       $http.post(
                           service.locationUrl + "/companystore/storename",
                           objStore)
                       .success(function () {
                           // the obj for insert
                           var storeObj = $scope.rowsStore;
                           console.log("obj Store", storeObj);
                           // insert method
                           $http.post(
                           service.locationUrl + "/companystore",
                           storeObj)
                       .success(function () {
                           $location.path($scope.path);
                       })
                       .error(function () { });
                           //    // end insert
                       })
                       .error(function () {
                           $('.popup-delete-save').bPopup().close();
                           $scope.titleExist = "This Store already exist";
                           $('.popup-exist').bPopup();
                       });
                   }
               }
               //End 
           }).error(function () {
               console.log("err");
           });   
    }
    ///End
    $scope.delete = function () {
        // popup detele
        $scope.title = "Do you want delete this Store ?";
        $('.popup-delete-save').bPopup(popupOptions);
        // End
        $scope.action = function () {
            //delete method delete store
            $http.delete(
                service.locationUrl + "/companystore/" + $routeParams.Id)
            .success(function () {
                $location.path($scope.path);
            })
            .error(function () { });
            // end put
        }
    }
}